/****************************************************************************
*                                                                           *
* Copyright (c) RSC. All rights reserved(2010).								*
* Developer: Marius Ionescu    						                        *
*                                                                           *
****************************************************************************/

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.Security;
using System.Web.UI.WebControls;
using NS_Controls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.UI;
using AjaxControlToolkit;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;


public partial class Investigatii : SessionPageBase
{
    public long longID
    {
        get { return (long)(this.ViewState["longID"] ?? 0); }
        set { this.ViewState["longID"] = value; }
    }

    public bool flagRef
    {
        get { return (bool)(this.ViewState["flagRef"] ?? false); }
        set { this.ViewState["flagRef"] = value; }
    }

    public string strINVID
    {
        get { return (string)(this.ViewState["strINVID"] ?? ""); }
        set { this.ViewState["strINVID"] = value; }
    }

    public string strNameINV
    {
        get { return (string)(this.ViewState["strNameINV"] ?? ""); }
        set { this.ViewState["strNameINV"] = value; }
    }

    public string strREFID
    {
        get { return (string)(this.ViewState["strREFID"] ?? ""); }
        set { this.ViewState["strREFID"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                LoadInvestigation();
                flagRef = false;
            }

            btnCloseUnits.OnClick = "$get('" + CancelButtonUM.ClientID + "').click();";
            txtNameGroup.Attributes.Add("onkeydown", @"
				if(event.which || event.keyCode)
					if ((event.which == 13) || (event.keyCode == 13))
					{
						document.getElementById('" + btnInvSearch.ClientID + @"_btn').click();
						return false;
					}
				return true;");
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void LoadUnits()
    {
        try
        {
            DataSet ds_Units = Hipocrate.DB.DBConnection.GetData("UM_Select", new string[] { }, new object[] { });
            DataView dv0 = ds_Units.Tables[0].DefaultView;
            rptUnits.DataSource = dv0;
            rptUnits.DataBind();
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void LoadInvestigation()
    {
        try
        {
            btnInvSearch.Text = Resources.DatabaseStrings.btn_Search + " Investigatie";
            btnInvAdd.Text = Resources.DatabaseStrings.btn_Add + " Investigatie";
            btnInvSave.Text = Resources.DatabaseStrings.btn_Save + " Investigatie";
            btnInvDelete.Text = Resources.DatabaseStrings.btn_Delete + " Investigatie";
            btnInvSave.Visible = false;
            btnInvDelete.Visible = false;
            btnInvAdd.Visible = true;

            btnSaveRefs.Text = Resources.DatabaseStrings.btn_Save + " Referinta";
            btnDeleteRefs.Text = Resources.DatabaseStrings.btn_Delete + " Referinta";
            btnSaveRefs.Visible = false;
            btnDeleteRefs.Visible = false;

            DataSet ds_InvestFilter = Hipocrate.DB.DBConnection.GetData("InvestigationFilter_Select", new string[] { }, new object[] { });
            DataView dv0 = ds_InvestFilter.Tables[0].DefaultView;
            DataView dv1 = ds_InvestFilter.Tables[1].DefaultView;
            DataView dv2 = ds_InvestFilter.Tables[2].DefaultView;

            drpTypeMachinesSearch.DataSource = dv0;
            drpTypeMachinesSearch.DataTextField = "name";
            drpTypeMachinesSearch.DataValueField = "id";
            drpTypeMachinesSearch.DataBind();
            drpTypeMachinesSearch.Items.Insert(0, "");

            drpGroupInvestigation.DataSource = dv1;
            drpGroupInvestigation.DataTextField = "name";
            drpGroupInvestigation.DataValueField = "id";
            drpGroupInvestigation.DataBind();
            drpGroupInvestigation.Items.Insert(0, "");

            drpTypeProbe.DataSource = dv2;
            drpTypeProbe.DataTextField = "name";
            drpTypeProbe.DataValueField = "id";
            drpTypeProbe.DataBind();
            drpTypeProbe.Items.Insert(0, "");

            drpActive.Items.Insert(0, "");
            drpActive.Items.Insert(1, "Da");
            drpActive.Items.Insert(2, "Nu");

        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void LoadUnitsID_Click(object sender, EventArgs e)
    {
		this.Page.Trace.Write("LoadUnitsID_Click");
		try
        {
            trWarning.Style.Add("display", "none");
            string strData = "";
            string js = "";
            if (sender is LinkButton)
            {
                strData = (sender as LinkButton).CommandArgument;
                js = "windowTop('" + (sender as LinkButton).ClientID + "')";
            }
            else
            {
                strData = (sender as ImageButton).CommandArgument;
                js = "windowTop('" + (sender as System.Web.UI.WebControls.Image).ClientID + "')";
            }
            string[] data = strData.Split(',');
            txtName.Text = data[0].Replace("<", "[").Replace(">", "]");
            txtSymbol.Text = data[1].Replace("<", "[").Replace(">", "]");
            longID = Convert.ToInt64(Server.HtmlEncode(data[2]));
            mdlPopupUnits.Show();
            upUnits.Update();

            ScriptManager.RegisterStartupScript(this, typeof(Investigatii), "windowTop", js, true);
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void LoadDetailsInvestigation_Click(object sender, EventArgs e)
    {
        try
        {
            btnInvSave.Visible = true;
            btnInvDelete.Visible = true;
            btnInvAdd.Visible = true;
            if (sender != null)
                strINVID = (sender as LinkButton).CommandArgument;
            divSearch.Style.Add("display", "none");
            divResultsSearch.Style.Add("display", "none");
            divInvestigationDetails.Style.Add("display", "block");
            divRefsDetails.Style.Add("display", "none");
            divRefs.Style.Add("display", "block");

            btnSaveRefs.Visible = false;
            btnDeleteRefs.Visible = false;

            DataSet ds_InvestData = Hipocrate.DB.DBConnection.GetData("select * from vAnalyseTypes where [id]=" + strINVID);
            DataView dv0 = ds_InvestData.Tables[0].DefaultView;

            strNameINV = dv0[0]["Name"].ToString();

            txtNameInvestDet.Text = Tools.GetSQLValue(dv0[0]["Name"].ToString()).ToString();
            txtCodIntern.Text = Tools.GetSQLValue(dv0[0]["Code"].ToString()).ToString();
            txtNameCAS.Text = Tools.GetSQLValue(dv0[0]["CASName"].ToString()).ToString();
            txtCodCAS.Text = Tools.GetSQLValue(dv0[0]["CASCode"].ToString()).ToString();

            DataSet ds_InvestFilter = Hipocrate.DB.DBConnection.GetData("InvestigationFilter_Select", new string[] { }, new object[] { });
            DataView dv1 = ds_InvestFilter.Tables[0].DefaultView;
            DataView dv2 = ds_InvestFilter.Tables[1].DefaultView;
            DataView dv3 = ds_InvestFilter.Tables[2].DefaultView;

            drpMachineDetails.DataSource = dv1;
            drpMachineDetails.DataTextField = "name";
            drpMachineDetails.DataValueField = "id";
            drpMachineDetails.DataBind();
            drpMachineDetails.Items.Insert(0, "");

            if (dv1[0]["ID"] != System.DBNull.Value)
            {
                foreach (ListItem li in drpMachineDetails.Items)
                {
                    if (0 == String.Compare(li.Value, dv0[0]["MachineTypeID"].ToString()))
                        drpMachineDetails.SelectedValue = li.Value;
                }
            }

            if (dv0[0]["Active"] != System.DBNull.Value)
                chkActive.Checked = Convert.ToBoolean(dv0[0]["Active"]);
            if (dv0[0]["AIClosed"] != System.DBNull.Value)
                chkIA.Checked = Convert.ToBoolean(dv0[0]["AIClosed"]);
            if (dv0[0]["IsPrintable"] != System.DBNull.Value)
                chkPrintable.Checked = Convert.ToBoolean(dv0[0]["IsPrintable"]);
            if (dv0[0]["Inline"] != System.DBNull.Value)
                chkResultsInline.Checked = Convert.ToBoolean(dv0[0]["Inline"]);
            if (dv0[0]["IsCASPayable"] != System.DBNull.Value)
                chkPaymentCAS.Checked = Convert.ToBoolean(dv0[0]["IsCASPayable"]);
            if (dv0[0]["DisplayHeader"] != System.DBNull.Value)
                chkTableHeader.Checked = Convert.ToBoolean(dv0[0]["DisplayHeader"]);

            drpInvestigationGroup.DataSource = dv2;
            drpInvestigationGroup.DataTextField = "name";
            drpInvestigationGroup.DataValueField = "id";
            drpInvestigationGroup.DataBind();
            drpInvestigationGroup.Items.Insert(0, "");

            if (dv2[0]["ID"] != System.DBNull.Value)
            {
                foreach (ListItem li in drpInvestigationGroup.Items)
                {
                    if (0 == String.Compare(li.Value, dv0[0]["GroupID"].ToString()))
                        drpInvestigationGroup.SelectedValue = li.Value;
                }
            }

            drpProbeType.DataSource = dv3;
            drpProbeType.DataTextField = "name";
            drpProbeType.DataValueField = "id";
            drpProbeType.DataBind();
            drpProbeType.Items.Insert(0, "");

            if (dv3[0]["ID"] != System.DBNull.Value)
            {
                foreach (ListItem li in drpProbeType.Items)
                {
                    if (0 == String.Compare(li.Value, dv0[0]["SampleTypeID"].ToString()))
                        drpProbeType.SelectedValue = li.Value;
                }
            }

            txtDescription.Text = Tools.GetSQLValue(dv0[0]["Description"].ToString()).ToString();
            txtXI.Text = Tools.GetSQLValue(dv0[0]["ScreenX"].ToString()).ToString();
            txtYI.Text = Tools.GetSQLValue(dv0[0]["ScreenY"].ToString()).ToString();
            txtZI.Text = Tools.GetSQLValue(dv0[0]["ScreenZ"].ToString()).ToString();
            txtXP.Text = Tools.GetSQLValue(dv0[0]["PrintX"].ToString()).ToString();
            txtYP.Text = Tools.GetSQLValue(dv0[0]["PrintY"].ToString()).ToString();
            txtZP.Text = Tools.GetSQLValue(dv0[0]["PrintZ"].ToString()).ToString();

            DataSet ds_SectionRestricted = Hipocrate.DB.DBConnection.GetData("select S.code, S.name from sections S join ANALYSE_RESTRICTED_SECTIONS A " +
                                                                                "on S.Code=A.SectionCode where A.AnalyseTypeID = " + strINVID + " order by name");
            DataView dv4 = ds_SectionRestricted.Tables[0].DefaultView;

            if (dv4.Count > 0)
            {
                for (int i = 0; i < dv4.Count; ++i)
                {
                    CheckBox chk = new CheckBox();
                    chk.Checked = true;
                    chk.ID = "chkS" + i;
                    Literal ltr = new Literal();
                    ltr.Text = dv4[i]["name"].ToString();
                    plcSectionsRestricted.Controls.Add(chk);
                    plcSectionsRestricted.Controls.Add(ltr);
                }
            }

            DataSet ds_SectionFree = Hipocrate.DB.DBConnection.GetData("select S.code, S.name from sections S where NOT EXISTS (select * from ANALYSE_RESTRICTED_SECTIONS A " +
                                                                                "where S.Code=A.SectionCode and A.AnalyseTypeID = " + strINVID + ") order by name");
            DataView dv5 = ds_SectionFree.Tables[0].DefaultView;

            if (dv5.Count > 0)
            {
                for (int i = 0; i < dv5.Count; ++i)
                {
                    CheckBox chk = new CheckBox();
                    chk.Checked = false;
                    chk.ID = "chkF" + i;
                    Literal ltr = new Literal();
                    ltr.Text = dv5[i]["name"].ToString();
                    plcSectionFree.Controls.Add(chk);
                    plcSectionFree.Controls.Add(ltr);
                }
            }

            lblDetailsInvest.Text = "Detalii investigatie: <span class=\"redFont\"><u>" + dv0[0]["Name"].ToString() + "</u></span>";

            if (Convert.ToBoolean(dv0[0]["refs"]) == false)
                trRefs.Visible = true;
            else
                trRefs.Visible = false;

            DataSet ds_Refs = Hipocrate.DB.DBConnection.GetData("select * from REFS where TypeID = " + strINVID + " order by ScreenY, ScreenX, [Name]");
            DataView dvRefs = ds_Refs.Tables[0].DefaultView;
            rptRefs.DataSource = dvRefs;
            rptRefs.DataBind();
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void LoadRefs_Click(object sender, EventArgs e)
    {
        try
        {
            btnInvSearch.Visible = true;
            btnInvSave.Visible = false;
            btnInvDelete.Visible = false;
            btnInvAdd.Visible = false;
            divSearch.Style.Add("display", "none");
            divResultsSearch.Style.Add("display", "none");
            divInvestigationDetails.Style.Add("display", "none");
            divRefsDetails.Style.Add("display", "block");
            btnSaveRefs.Visible = true;
            btnDeleteRefs.Visible = true;

            if (sender != null)
                strREFID = (sender as LinkButton).CommandArgument;

            DataSet ds_InvestData = Hipocrate.DB.DBConnection.GetData("select * from vAnalyseTypes where [id]=" + strINVID);
            DataView dv0 = ds_InvestData.Tables[0].DefaultView;

            lnkButtonRef.CommandArgument = strINVID;
            lnkRefDetails.CommandArgument = strINVID;

            DataSet ds_RefsData = Hipocrate.DB.DBConnection.GetData("select * from REFS where [id]=" + strREFID);
            DataView dv1 = ds_RefsData.Tables[0].DefaultView;

            lblName.Text = dv0[0]["Name"].ToString();
            txtRefName.Text = dv1[0]["Name"].ToString();
            txtRefCod.Text = dv1[0]["Code"].ToString();

            DataSet ds_RefUM = Hipocrate.DB.DBConnection.GetData("select [id], [name] from UM order by [name]");
            DataView dv2 = ds_RefUM.Tables[0].DefaultView;
            drpUnitRefs.DataSource = dv2;
            drpUnitRefs.DataTextField = "name";
            drpUnitRefs.DataValueField = "id";
            drpUnitRefs.DataBind();
            drpUnitRefs.Items.Insert(0, "");

            if (dv2[0]["ID"] != System.DBNull.Value)
            {
                foreach (ListItem li in drpUnitRefs.Items)
                {
                    if (0 == String.Compare(li.Value, dv1[0]["id"].ToString()))
                        drpUnitRefs.SelectedValue = li.Value;
                }
            }

            drpResultRef.Items.Insert(0, "");
            drpResultRef.Items.Insert(1, "Numar");
            drpResultRef.Items[1].Value = "INT";
            drpResultRef.Items.Insert(2, "Text simplu");
            drpResultRef.Items[2].Value = "STR";
            drpResultRef.Items.Insert(3, "Text compus");
            drpResultRef.Items[3].Value = "TXT";
            drpResultRef.Items.Insert(4, "Grafic");
            drpResultRef.Items[4].Value = "FUN";
            drpResultRef.Items.Insert(5, "Imagine");
            drpResultRef.Items[5].Value = "IMG";
            drpResultRef.Items.Insert(6, "Lista");
            drpResultRef.Items[6].Value = "LST";

            foreach (ListItem li in drpResultRef.Items)
            {
                if (0 == String.Compare(li.Value, dv1[0]["ValueType"].ToString()))
                    drpResultRef.SelectedValue = li.Value;
            }

            txtListRefs.Text = dv1[0]["ValueList"].ToString();
            if (dv1[0]["ValueMultiple"] != System.DBNull.Value)
                chkSelMultipleRef.Checked = Convert.ToBoolean(dv1[0]["ValueMultiple"]);
            if (dv1[0]["IsPrintable"] != System.DBNull.Value)
                chkPrintableRef.Checked = Convert.ToBoolean(dv1[0]["IsPrintable"]);
            if (dv1[0]["Active"] != System.DBNull.Value)
                chkACtiveRef.Checked = Convert.ToBoolean(dv1[0]["Active"]);

            txtSRX.Text = dv1[0]["ScreenX"].ToString();
            txtSRY.Text = dv1[0]["ScreenY"].ToString();
            txtSRZ.Text = dv1[0]["ScreenZ"].ToString();
            txtPRX.Text = dv1[0]["PrintX"].ToString();
            txtPRY.Text = dv1[0]["PrintY"].ToString();
            txtPRZ.Text = dv1[0]["PrintZ"].ToString();

            lblRefs.Text = "Rezultate referinta pentru investigatia: <span class=\"redFont\"><u>" + dv0[0]["Name"].ToString() + "</u><span class=\"backFont\">&nbsp;(Click pentru a reveni la investigatia selectata.)</span></span>";

            if (sender != null || flagRef == true)
            {
                string js = "displayIntervals('" + strREFID + "')";
                ScriptManager.RegisterStartupScript(this, typeof(Investigatii), "displayIntervals", js, true);

                string js2 = "displayType('" + strREFID + "')";
                ScriptManager.RegisterStartupScript(this, typeof(Investigatii), "displayType", js2, true);
            }

            DataSet ds_RefTypes = Hipocrate.DB.DBConnection.GetData("select * from REFRESULTS_TYPES");
            DataView dvTypes = ds_RefTypes.Tables[0].DefaultView;
            drpTypesRefs.DataSource = dvTypes;
            drpTypesRefs.DataTextField = "name";
            drpTypesRefs.DataValueField = "id";
            drpTypesRefs.DataBind();
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnSaveInvestigation_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txtNameInvestDet.Text) || string.IsNullOrEmpty(drpMachineDetails.SelectedValue) || string.IsNullOrEmpty(drpInvestigationGroup.SelectedValue) || string.IsNullOrEmpty(drpProbeType.SelectedValue))
            {
                this.SetMessage("Completati campurile marcate cu rosu!", true);
                if (string.IsNullOrEmpty(strINVID))
                    btnAddInvestigation_Click(null, null);
                else
                    LoadDetailsInvestigation_Click(null, null);
                return;
            }

            if (!string.IsNullOrEmpty(strINVID))
            {
                DataSet ds_SectionRestricted = Hipocrate.DB.DBConnection.GetData("select S.code, S.name from sections S join ANALYSE_RESTRICTED_SECTIONS A " +
                                                                                    "on S.Code=A.SectionCode where A.AnalyseTypeID = " + strINVID + " order by name");
                DataView dv4 = ds_SectionRestricted.Tables[0].DefaultView;

                if (dv4.Count > 0)
                {
                    for (int i = 0; i < dv4.Count; ++i)
                    {
                        CheckBox chk = new CheckBox();
                        chk.Checked = true;
                        chk.ID = "chkS" + i;
                        chk.ToolTip = dv4[i]["code"].ToString();
                        Literal ltr = new Literal();
                        ltr.Text = dv4[i]["name"].ToString();
                        plcSectionsRestricted.Controls.Add(chk);
                        plcSectionsRestricted.Controls.Add(ltr);
                    }
                }

                DataSet ds_SectionFree = Hipocrate.DB.DBConnection.GetData("select S.code, S.name from sections S where NOT EXISTS (select * from ANALYSE_RESTRICTED_SECTIONS A " +
                                                                                    "where S.Code=A.SectionCode and A.AnalyseTypeID = " + strINVID + ") order by name");
                DataView dv5 = ds_SectionFree.Tables[0].DefaultView;

                if (dv5.Count > 0)
                {
                    for (int i = 0; i < dv5.Count; ++i)
                    {
                        CheckBox chk = new CheckBox();
                        chk.Checked = false;
                        chk.ID = "chkF" + i;
                        chk.ToolTip = dv5[i]["code"].ToString();
                        Literal ltr = new Literal();
                        ltr.Text = dv5[i]["name"].ToString();
                        plcSectionFree.Controls.Add(chk);
                        plcSectionFree.Controls.Add(ltr);
                    }
                }

                foreach (Control ctrl in plcSectionsRestricted.Controls)
                {
                    if (ctrl is CheckBox)
                    {
                        (ctrl as CheckBox).Checked = "on".Equals(this.Request.Form[ctrl.UniqueID]);
                        int n;
                        if ((ctrl as CheckBox).Checked == false)
                        {
                            n = Hipocrate.DB.DBConnection.Execute("delete ANALYSE_RESTRICTED_SECTIONS where AnalyseTypeID=" + strINVID + " AND SectionCode='" + (ctrl as CheckBox).ToolTip + "'");
                        }
                    }
                }

                foreach (Control ctrl in plcSectionFree.Controls)
                {
                    if (ctrl is CheckBox)
                    {
                        (ctrl as CheckBox).Checked = "on".Equals(this.Request.Form[ctrl.UniqueID]);
                        int n;
                        if ((ctrl as CheckBox).Checked == true)
                            n = Hipocrate.DB.DBConnection.Execute("insert into ANALYSE_RESTRICTED_SECTIONS (AnalyseTypeID,SectionCode) values(" + strINVID + ", '" + (ctrl as CheckBox).ToolTip + "')");
                    }
                }

                plcSectionsRestricted.Controls.Clear();
                plcSectionFree.Controls.Clear();

                int x;
                x = Hipocrate.DB.DBConnection.Execute("AnalyseType_Save",
                  new string[] { "intCodeID",
	                    "strName","strCode","strCASName","strCASCode",
	                    "strMachineType", "strActive","strAIClosed",
                        "strPrintable","strInline","strCASPayable",
	                    "strDisplayHeader","strGroup", "strSampleType","strDescription",
                        "strScreenX", "strScreenY","strScreenZ",
                        "strPrintX","strPrintY","strPrintZ", "hasNormalValues"},
                  new object[] { strINVID, Tools.GetSQLValue(txtNameInvestDet.Text), Tools.GetSQLValue(txtCodIntern.Text), Tools.GetSQLValue(txtNameCAS.Text),
                        Tools.GetSQLValue(txtCodCAS.Text),Tools.GetSQLValue(drpMachineDetails.SelectedValue),chkActive.Checked,chkIA.Checked,
                        chkPrintable.Checked,chkResultsInline.Checked, chkPaymentCAS.Checked, chkTableHeader.Checked,
                        Tools.GetSQLValue(drpInvestigationGroup.SelectedValue),Tools.GetSQLValue(drpProbeType.SelectedValue),Tools.GetSQLValue(txtDescription.Text),
                        Tools.GetSQLValue(txtXI.Text),Tools.GetSQLValue(txtYI.Text),Tools.GetSQLValue(txtZI.Text),
                        Tools.GetSQLValue(txtXP.Text),Tools.GetSQLValue(txtYP.Text),Tools.GetSQLValue(txtZP.Text), chkNormalValues.Checked });
            }
            else
            {
                object outputValue = Hipocrate.DB.DBConnection.ExecuteSPWithInOutParams("AnalyseType_Save",
                  new string[] {
	                    "strName","strCode","strCASName","strCASCode",
	                    "strMachineType", "strActive","strAIClosed",
                        "strPrintable","strInline","strCASPayable",
	                    "strDisplayHeader","strGroup", "strSampleType","strDescription",
                        "strScreenX", "strScreenY","strScreenZ",
                        "strPrintX","strPrintY","strPrintZ"},
                  new object[] { Tools.GetSQLValue(txtNameInvestDet.Text), Tools.GetSQLValue(txtCodIntern.Text), Tools.GetSQLValue(txtNameCAS.Text),
                        Tools.GetSQLValue(txtCodCAS.Text),Tools.GetSQLValue(drpMachineDetails.SelectedValue),chkActive.Checked,chkIA.Checked,
                        chkPrintable.Checked,chkResultsInline.Checked, chkPaymentCAS.Checked, chkTableHeader.Checked,
                        Tools.GetSQLValue(drpInvestigationGroup.SelectedValue),Tools.GetSQLValue(drpProbeType.SelectedValue),Tools.GetSQLValue(txtDescription.Text),
                        Tools.GetSQLValue(txtXI.Text),Tools.GetSQLValue(txtYI.Text),Tools.GetSQLValue(txtZI.Text),
                        Tools.GetSQLValue(txtXP.Text),Tools.GetSQLValue(txtYP.Text),Tools.GetSQLValue(txtZP.Text) }, "@strOUT")[0].Value;

                strINVID = outputValue.ToString();

                DataSet ds_SectionFree = Hipocrate.DB.DBConnection.GetData("select S.code, S.name from sections S where NOT EXISTS (select * from ANALYSE_RESTRICTED_SECTIONS A " +
                                                                                    "where S.Code=A.SectionCode and A.AnalyseTypeID = " + strINVID + ") order by name");
                DataView dvF = ds_SectionFree.Tables[0].DefaultView;

                if (dvF.Count > 0)
                {
                    for (int i = 0; i < dvF.Count; ++i)
                    {
                        CheckBox chk = new CheckBox();
                        chk.Checked = false;
                        chk.ID = "chkF" + i;
                        chk.ToolTip = dvF[i]["code"].ToString();
                        Literal ltr = new Literal();
                        ltr.Text = dvF[i]["name"].ToString();
                        plcSectionFree.Controls.Add(chk);
                        plcSectionFree.Controls.Add(ltr);
                    }
                }

                foreach (Control ctrl in plcSectionFree.Controls)
                {
                    if (ctrl is CheckBox)
                    {
                        (ctrl as CheckBox).Checked = "on".Equals(this.Request.Form[ctrl.UniqueID]);
                        int n;
                        if ((ctrl as CheckBox).Checked == true)
                            n = Hipocrate.DB.DBConnection.Execute("insert into ANALYSE_RESTRICTED_SECTIONS (AnalyseTypeID,SectionCode) values(" + strINVID + ", '" + (ctrl as CheckBox).ToolTip + "')");
                    }
                }
            }

            plcSectionFree.Controls.Clear();

            LoadDetailsInvestigation_Click(null, null);
            this.SetMessage("Datele au fost salvate!", false);
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            btnInvSearch.Visible = true;
            btnInvSave.Visible = false;
            btnInvDelete.Visible = false;
            btnInvAdd.Visible = true;
            divSearch.Style.Add("display", "block");
            divResultsSearch.Style.Add("display", "block");
            divInvestigationDetails.Style.Add("display", "none");
            divRefsDetails.Style.Add("display", "none");

            btnSaveRefs.Visible = false;
            btnDeleteRefs.Visible = false;

            string tempActive = "";
            if (drpActive.SelectedValue == "")
                tempActive = "";
            else if (drpActive.SelectedValue == "Da")
                tempActive = "1";
            else
                tempActive = "0";
            DataSet ds_Units = Hipocrate.DB.DBConnection.GetData("SearchInvestigation", new string[] { "Code", "GroupID", "SampleTypeID", "Active", "MachineTypeID", },
                new object[] { Tools.GetSQLValue(txtNameGroup.Text), Tools.GetSQLValue(drpGroupInvestigation.SelectedValue), Tools.GetSQLValue(drpTypeProbe.SelectedValue), Tools.GetSQLValue(tempActive), Tools.GetSQLValue(drpTypeMachinesSearch.SelectedValue) });
            DataView dv0 = ds_Units.Tables[0].DefaultView;

            rptInvestigation.DataSource = dv0;
            rptInvestigation.DataBind();

            divResultsSearch.Style.Add("display", "block");
            lblCount.Text = dv0.Count + " investigatii gasite.";
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnAddInvestigation_Click(object sender, EventArgs e)
    {
        try
        {
            btnInvAdd.Visible = false;
            btnInvSave.Visible = true;
            btnInvDelete.Visible = false;
            strINVID = null;
            divSearch.Style.Add("display", "none");
            divResultsSearch.Style.Add("display", "none");
            divInvestigationDetails.Style.Add("display", "block");
            divRefs.Style.Add("display", "none");
            lblDetailsInvest.Text = "Detalii investigatie:";

            btnSaveRefs.Visible = false;
            btnDeleteRefs.Visible = false;

            txtNameInvestDet.Text = "";
            txtCodIntern.Text = "";
            txtNameCAS.Text = "";
            txtCodCAS.Text = "";
            txtDescription.Text = "";
            chkActive.Checked = false;
            chkIA.Checked = false;
            chkPrintable.Checked = false;
            chkResultsInline.Checked = false;
            chkPaymentCAS.Checked = false;
            chkTableHeader.Checked = false;
            txtXI.Text = "";
            txtYI.Text = "";
            txtZI.Text = "";
            txtXP.Text = "";
            txtYP.Text = "";
            txtZP.Text = "";

            DataSet ds_InvestFilter = Hipocrate.DB.DBConnection.GetData("InvestigationFilter_Select", new string[] { }, new object[] { });
            DataView dv1 = ds_InvestFilter.Tables[0].DefaultView;
            DataView dv2 = ds_InvestFilter.Tables[1].DefaultView;
            DataView dv3 = ds_InvestFilter.Tables[2].DefaultView;

            drpMachineDetails.DataSource = dv1;
            drpMachineDetails.DataTextField = "name";
            drpMachineDetails.DataValueField = "id";
            drpMachineDetails.DataBind();
            drpMachineDetails.Items.Insert(0, "");

            drpInvestigationGroup.DataSource = dv2;
            drpInvestigationGroup.DataTextField = "name";
            drpInvestigationGroup.DataValueField = "id";
            drpInvestigationGroup.DataBind();
            drpInvestigationGroup.Items.Insert(0, "");

            drpProbeType.DataSource = dv3;
            drpProbeType.DataTextField = "name";
            drpProbeType.DataValueField = "id";
            drpProbeType.DataBind();
            drpProbeType.Items.Insert(0, "");

            DataSet ds_SectionFree = Hipocrate.DB.DBConnection.GetData("select S.code, S.name from sections S order by name");
            DataView dvF = ds_SectionFree.Tables[0].DefaultView;

            if (dvF.Count > 0)
            {
                for (int i = 0; i < dvF.Count; ++i)
                {
                    CheckBox chk = new CheckBox();
                    chk.Checked = false;
                    chk.ID = "chkF" + i;
                    chk.ToolTip = dvF[i]["code"].ToString();
                    Literal ltr = new Literal();
                    ltr.Text = dvF[i]["name"].ToString();
                    plcSectionFree.Controls.Add(chk);
                    plcSectionFree.Controls.Add(ltr);
                }
            }
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnDeleteInvestigation_Click(object sender, EventArgs e)
    {
        try
        {
            int x = Hipocrate.DB.DBConnection.Execute("AnalyseType_Delete", new string[] { "intCodeID" }, new object[] { Tools.GetSQLValue(strINVID) });
            btnAddInvestigation_Click(null, null);
            this.SetMessage("Investigatia a fost stearsa.", false);
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnAddRefs_Click(object sender, EventArgs e)
    {
        try
        {
            btnInvSearch.Visible = true;
            btnInvSave.Visible = false;
            btnInvDelete.Visible = false;
            btnInvAdd.Visible = false;
            divSearch.Style.Add("display", "none");
            divResultsSearch.Style.Add("display", "none");
            divInvestigationDetails.Style.Add("display", "none");
            divRefsDetails.Style.Add("display", "block");
            btnSaveRefs.Visible = true;
            btnDeleteRefs.Visible = false;

            lnkButtonRef.CommandArgument = strINVID;
            lnkRefDetails.CommandArgument = strINVID;

            strREFID = null;
            flagRef = false;

            lblName.Text = "";
            txtRefName.Text = "";
            txtRefCod.Text = "";

            DataSet ds_RefUM = Hipocrate.DB.DBConnection.GetData("select [id], [name] from UM order by [name]");
            DataView dv2 = ds_RefUM.Tables[0].DefaultView;
            drpUnitRefs.DataSource = dv2;
            drpUnitRefs.DataTextField = "name";
            drpUnitRefs.DataValueField = "id";
            drpUnitRefs.DataBind();
            drpUnitRefs.Items.Insert(0, "");

            drpResultRef.Items.Insert(0, "");
            drpResultRef.Items.Insert(1, "Numar");
            drpResultRef.Items[1].Value = "INT";
            drpResultRef.Items.Insert(2, "Text simplu");
            drpResultRef.Items[2].Value = "STR";
            drpResultRef.Items.Insert(3, "Text compus");
            drpResultRef.Items[3].Value = "TXT";
            drpResultRef.Items.Insert(4, "Grafic");
            drpResultRef.Items[4].Value = "FUN";
            drpResultRef.Items.Insert(5, "Imagine");
            drpResultRef.Items[5].Value = "IMG";
            drpResultRef.Items.Insert(6, "Lista");
            drpResultRef.Items[6].Value = "LST";

            txtListRefs.Text = "";
            chkSelMultipleRef.Checked = false;
            chkPrintableRef.Checked = false;
            chkACtiveRef.Checked = false;

            txtSRX.Text = "";
            txtSRY.Text = "";
            txtSRZ.Text = "";
            txtPRX.Text = "";
            txtPRY.Text = "";
            txtPRZ.Text = "";

            lblRefs.Text = "Rezultate referinta pentru investigatia: <span class=\"redFont\"><u>" + strNameINV + "</u><span class=\"backFont\">&nbsp;(Click pentru a reveni la investigatia selectata.)</span></span>";

            string js = "resetArray()";
            ScriptManager.RegisterStartupScript(this, typeof(Investigatii), "resetArray", js, true);

            DataSet ds_RefTypes = Hipocrate.DB.DBConnection.GetData("select * from REFRESULTS_TYPES");
            DataView dvTypes = ds_RefTypes.Tables[0].DefaultView;
            drpTypesRefs.DataSource = dvTypes;
            drpTypesRefs.DataTextField = "name";
            drpTypesRefs.DataValueField = "id";
            drpTypesRefs.DataBind();
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnSaveRefs_Click(object sender, EventArgs e)
    {
        try
        {
            string jsReset = "resetArray()";
            if (string.IsNullOrEmpty(txtRefName.Text) || string.IsNullOrEmpty(txtRefCod.Text))
            {
                this.SetMessage("Completati numele!", true);
                ScriptManager.RegisterStartupScript(this, typeof(Investigatii), "resetArray", jsReset, true);
                return;
            }

            if (!string.IsNullOrEmpty(strREFID))
            {
                int x;
                x = Hipocrate.DB.DBConnection.Execute("Refs_Save",
                  new string[] { "intCodeID",
	                    "strAnalyseType","strUMID","strName","strCode",
	                    "strValueType", "strValueList","strValueMultiple",
                        "strPrintable","strScreenX","strScreenY","strScreenZ",
	                    "strPrintX","strPrintY","strPrintZ", "strActive"},
                  new object[] { strREFID, strINVID,Tools.GetSQLValue(drpUnitRefs.SelectedValue),
                        Tools.GetSQLValue(txtRefName.Text), Tools.GetSQLValue(txtRefCod.Text), 
                        Tools.GetSQLValue(drpResultRef.SelectedValue), Tools.GetSQLValue(txtListRefs.Text),
                        chkSelMultipleRef.Checked, chkPrintableRef.Checked, 
                        Tools.GetSQLValue(txtSRX.Text),Tools.GetSQLValue(txtSRY.Text),Tools.GetSQLValue(txtSRZ.Text),
                        Tools.GetSQLValue(txtPRX.Text),Tools.GetSQLValue(txtPRY.Text),Tools.GetSQLValue(txtPRZ.Text),
                        chkACtiveRef.Checked });
                flagRef = false;
            }
            else
            {
                object outputValue = Hipocrate.DB.DBConnection.ExecuteSPWithInOutParams("Refs_Save",
                  new string[] {
	                    "strAnalyseType","strUMID","strName","strCode",
	                    "strValueType", "strValueList","strValueMultiple",
                        "strPrintable","strScreenX","strScreenY","strScreenZ",
	                    "strPrintX","strPrintY","strPrintZ", "strActive"},
                  new object[] { strINVID, Tools.GetSQLValue(drpUnitRefs.SelectedValue),
                        Tools.GetSQLValue(txtRefName.Text), Tools.GetSQLValue(txtRefCod.Text), 
                        Tools.GetSQLValue(drpResultRef.SelectedValue), Tools.GetSQLValue(txtListRefs.Text),
                        chkSelMultipleRef.Checked, chkPrintableRef.Checked, 
                        Tools.GetSQLValue(txtSRX.Text),Tools.GetSQLValue(txtSRY.Text),Tools.GetSQLValue(txtSRZ.Text),
                        Tools.GetSQLValue(txtPRX.Text),Tools.GetSQLValue(txtPRY.Text),Tools.GetSQLValue(txtPRZ.Text),
                        chkACtiveRef.Checked }, "@strOUT")[0].Value;

                strREFID = outputValue.ToString();
                flagRef = false;
            }

            string js = "saveInterval(" + strREFID + ")";
            ScriptManager.RegisterStartupScript(this, typeof(Investigatii), "saveInterval", js, true);

            string js2 = "saveType(" + strREFID + ")";
            ScriptManager.RegisterStartupScript(this, typeof(Investigatii), "saveType", js2, true);

            LoadRefs_Click(null, null);
            this.SetMessage("Datele au fost salvate", false);
        }

        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnDeleteRefs_Click(object sender, EventArgs e)
    {
        try
        {
            int n = Hipocrate.DB.DBConnection.Execute("Refs_Delete", new string[] { "intCodeID" }, new object[] { strREFID });
            LoadDetailsInvestigation_Click(null, null);
            this.SetMessage("Referinta a fost stearsa.", false);
        }
        catch (Exception ex)
        {
            LoadDetailsInvestigation_Click(null, null);
            this.SetMessage(ex.Message, true);
        }
    }

    public void btnSaveUnits_Click(object sender, EventArgs e)
    {
        try
        {
            string x1 = txtName.Text.Replace("<", "[").Replace(">", "]");
            string x2 = txtSymbol.Text.Replace("<", "[").Replace(">", "]");

            if (string.IsNullOrEmpty(x1) || string.IsNullOrEmpty(x2))
            {
                trWarning.Style.Add("display", "block");
                return;
            }

            int n;
            if (longID != 0)
                n = Hipocrate.DB.DBConnection.Execute("UM_Save", new string[] { "intCodeID", "strName", "strSymbol" }, new object[] { longID, txtName.Text, txtSymbol.Text });
            else
                n = Hipocrate.DB.DBConnection.Execute("UM_Save", new string[] { "strName", "strSymbol" }, new object[] { txtName.Text, txtSymbol.Text });

            DataSet ds_Units = Hipocrate.DB.DBConnection.GetData("UM_Select", new string[] { }, new object[] { });
            DataView dv0 = ds_Units.Tables[0].DefaultView;
            rptUnits.DataSource = dv0;
            rptUnits.DataBind();
            mdlPopupUnits.Hide();

            this.SetMessage("Datele au fost salvate.", false);
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    public void btnAddUnits_Click(object sender, EventArgs e)
    {
        try
        {
            trWarning.Style.Add("display", "none");
            txtName.Text = "";
            txtSymbol.Text = "";
            longID = 0;
            upUnits.Update();
            mdlPopupUnits.Show();
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    public void btnDeleteUnits_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds_UnitsDelete = Hipocrate.DB.DBConnection.GetData("UM_Delete", new string[] { "intCodeID" }, new object[] { longID });

            DataSet ds_Units = Hipocrate.DB.DBConnection.GetData("UM_Select", new string[] { }, new object[] { });
            DataView dv0 = ds_Units.Tables[0].DefaultView;
            rptUnits.DataSource = dv0;
            rptUnits.DataBind();
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    private void LoadResults(bool bForce)
    {
        try
        {
            DataSet ds_Results = GetSession("ds_Results") as DataSet;
            if (ds_Results == null || bForce)
            {
                ds_Results = Hipocrate.DB.DBConnection.GetData("select * from REFRESULTS_TYPES order by [name]");

                foreach (DataTable dt in ds_Results.Tables)
                    foreach (DataColumn dc in dt.Columns)
                    {
                        dc.ReadOnly = false;
                        dc.AllowDBNull = true;
                    }

                SetSession("ds_Results", ds_Results);
            }

            if (ds_Results != null)
            {
                DataView dv = ds_Results.Tables[0].DefaultView;
                grResults.SessionKey = GetSessionKey("ds_Results");
                grResults.SessionKeyIndex = 0;
                grResults.SessionKeyFilter = dv.RowFilter;
                grResults.SessionKeyColumnHeadersIndex = 1;
                grResults.EnableEdit = true;
                grResults.DataSource = dv;
                grResults.DataBind();
            }
        }

        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (!grResults.showEditButton && !grResults.showDeleteButton)
            {
                TableCell cell0 = e.Row.Cells[1];
                cell0.Width = new Unit("1%");
                cell0.Style["border-right"] = "2px solid #666666";
                cell0.BackColor = System.Drawing.Color.LightGray;
                TableCell cell1 = e.Row.Cells[2];
                cell1.Width = new Unit("2%");
                cell1.Style["border-right"] = "2px solid #666666";
                cell1.BackColor = System.Drawing.Color.LightGray;
            }
            else if (!grResults.showEditButton || grResults.showDeleteButton)
            {
                TableCell cell0 = e.Row.Cells[0];
                cell0.Width = new Unit("1%");
                cell0.Style["border-right"] = "2px solid #666666";
                cell0.BackColor = System.Drawing.Color.LightGray;
                TableCell cell1 = e.Row.Cells[1];
                cell1.Width = new Unit("2%");
            }

            if (!grProbes.showEditButton && !grProbes.showDeleteButton)
            {
                TableCell cell0 = e.Row.Cells[1];
                cell0.Width = new Unit("1%");
                cell0.Style["border-right"] = "2px solid #666666";
                cell0.BackColor = System.Drawing.Color.LightGray;
                TableCell cell1 = e.Row.Cells[2];
                cell1.Width = new Unit("2%");
                cell1.Style["border-right"] = "2px solid #666666";
                cell1.BackColor = System.Drawing.Color.LightGray;
            }
            else if (!grProbes.showEditButton || grProbes.showDeleteButton)
            {
                TableCell cell0 = e.Row.Cells[0];
                cell0.Width = new Unit("1%");
                cell0.Style["border-right"] = "2px solid #666666";
                cell0.BackColor = System.Drawing.Color.LightGray;
                TableCell cell1 = e.Row.Cells[1];
                cell1.Width = new Unit("2%");
            }
        }
    }

    protected void imgDelete_Click(object sender, EventArgs e)
    {
        ImageButton btnDelete = sender as ImageButton;
        GridViewRow row = (GridViewRow)btnDelete.NamingContainer;

        DataView dv = new DataView((GetSessionDataSet("ds_Results")).Tables[0]);
        dv.RowFilter = "ID = " + Convert.ToString(this.grResults.DataKeys[row.RowIndex].Value);
        dv[0].Row.Delete();

        grResults.DataBind();
    }

    
    public void btnSaveResults_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds_Results = GetSessionDataSet("ds_Results");
            DataView dv = ds_Results.Tables[0].DefaultView;

            if (ds_Results != null)
            {
                Hipocrate.DB.DBConnection.SaveAll(ds_Results,
                    new object[] { 
						new object[] { "RefResults_Types_Save", new string[] { "Name" } }
					},
                    new object[] { 
						new object[] { "RefResults_Types_Save", new string[] { "ID", "Name" } }
					},
                    new object[] { 
						new object[] { "RefResults_Types_Delete", new string[] { "ID" } }
					}
                    );
                this.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
                LoadResults(true);
            }
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    private void LoadProbes(bool bForce)
    {
        try
        {
            DataSet ds_Probes = GetSession("ds_Probess") as DataSet;
            if (ds_Probes == null || bForce)
            {
                ds_Probes = Hipocrate.DB.DBConnection.GetData("select * from SAMPLE_TYPES order by [name]");

                foreach (DataTable dt in ds_Probes.Tables)
                    foreach (DataColumn dc in dt.Columns)
                    {
                        dc.ReadOnly = false;
                        dc.AllowDBNull = true;
                    }

                SetSession("ds_Probes", ds_Probes);
            }

            if (ds_Probes != null)
            {
                DataView dv = ds_Probes.Tables[0].DefaultView;
                grProbes.SessionKey = GetSessionKey("ds_Probes");
                grProbes.SessionKeyIndex = 0;
                grProbes.SessionKeyFilter = dv.RowFilter;
                grProbes.SessionKeyColumnHeadersIndex = 1;
                grProbes.EnableEdit = true;
                grProbes.DataSource = dv;
                grProbes.DataBind();
            }
        }

        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void imgDeleteProbes_Click(object sender, EventArgs e)
    {
        ImageButton btnDelete = sender as ImageButton;
        GridViewRow row = (GridViewRow)btnDelete.NamingContainer;

        DataView dv = new DataView((GetSessionDataSet("ds_Probes")).Tables[0]);
        dv.RowFilter = "ID = " + Convert.ToString(this.grProbes.DataKeys[row.RowIndex].Value);
        dv[0].Row.Delete();

        grProbes.DataBind();
    }


    public void btnSaveProbes_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds_Probes = GetSessionDataSet("ds_Probes");
            DataView dv = ds_Probes.Tables[0].DefaultView;

            if (ds_Probes != null)
            {
                Hipocrate.DB.DBConnection.SaveAll(ds_Probes,
                    new object[] { 
						new object[] { "SampleType_Save", new string[] { "Name", "Code" } }
					},
                    new object[] { 
						new object[] { "SampleType_Save", new string[] { "ID", "Name", "Code"  } }
					},
                    new object[] { 
						new object[] { "SampleType_Delete", new string[] { "ID" } }
					}
                    );
                this.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
                LoadResults(true);
            }
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void tabActive_TabChanged(object sender, EventArgs e)
    {
        if (tabContainer.ActiveTabIndex == 0)
        {
            btnSearch_Click(null, null);
        }
        if (tabContainer.ActiveTabIndex == 1)
            LoadResults(false);
        if (tabContainer.ActiveTabIndex == 2)
            LoadProbes(false);
        if (tabContainer.ActiveTabIndex == 3)
            LoadUnits();
    }

    [WebMethod, ScriptMethod]
    public static string[] getNormalValues(int RefID)
    {
        DataSet ds_getNormalValues = Hipocrate.DB.DBConnection.GetData("SELECT * FROM REF_NORMAL_VALUES WHERE RefID = " + RefID);
        DataView dv = ds_getNormalValues.Tables[0].DefaultView;

        string[] dataValues = new string[dv.Count];
        for (int i = 0; i < dv.Count; ++i)
            dataValues.SetValue(dv.Table.Rows[i]["ID"].ToString() + ";" + dv.Table.Rows[i]["RefID"].ToString() + ";" +
                                dv.Table.Rows[i]["SEX"].ToString() + ";" + dv.Table.Rows[i]["ageLo"].ToString() + ";" +
                                dv.Table.Rows[i]["ageHi"].ToString() + ";" + dv.Table.Rows[i]["MinValue"].ToString() + ";" +
                                dv.Table.Rows[i]["MaxValue"].ToString() + ";" + dv.Table.Rows[i]["Pregnant"].ToString() + ";", i);
        return dataValues;
    }

    [WebMethod, ScriptMethod]
    public static string[] insertNormalValues(object dataValues, string RefID)
    {
        object[] newArr = (object[])dataValues;

        for (int i = 0; i < newArr.Length; ++i)
        {
            Dictionary<string, object> tempDic = newArr[i] as Dictionary<string, object>;

            int n = Hipocrate.DB.DBConnection.Execute("REF_ADD_NormalValue",
            new string[] { "UID", "refID", "sex", "ageLo", "ageHi", "minValue", "maxValue", "Delete" },
            new object[] { tempDic["UID"], RefID, tempDic["sex"], tempDic["ageLo"], tempDic["ageHi"], tempDic["minValue"], tempDic["maxValue"], tempDic["toDelete"] });
        }

        DataSet ds_getNormalValues = Hipocrate.DB.DBConnection.GetData("SELECT * FROM REF_NORMAL_VALUES WHERE RefID = " + RefID);
        DataView dv = ds_getNormalValues.Tables[0].DefaultView;

        string[] returnValues = new string[dv.Count];
        for (int i = 0; i < dv.Count; ++i)
            returnValues.SetValue(dv.Table.Rows[i]["ID"].ToString() + ";" + dv.Table.Rows[i]["RefID"].ToString() + ";" +
                                dv.Table.Rows[i]["SEX"].ToString() + ";" + dv.Table.Rows[i]["ageLo"].ToString() + ";" +
                                dv.Table.Rows[i]["ageHi"].ToString() + ";" + dv.Table.Rows[i]["MinValue"].ToString() + ";" +
                                dv.Table.Rows[i]["MaxValue"].ToString() + ";" + dv.Table.Rows[i]["Pregnant"].ToString() + ";", i);
        return returnValues;
    }

    [WebMethod, ScriptMethod]
    public static string[] getType(string RefID)
    {
        DataSet ds_getType = Hipocrate.DB.DBConnection.GetData("select * from vRefResults_Types_Refs where [RefID]=" + RefID);
        DataView dv = ds_getType.Tables[0].DefaultView;

        string[] dataTypes = new string[dv.Count];
        for (int i = 0; i < dv.Count; ++i)
            dataTypes[i] = dv.Table.Rows[i]["TypeName"].ToString() + ";" + dv.Table.Rows[i]["TypeId"].ToString() + ";";
        return dataTypes;
    }

    [WebMethod, ScriptMethod]
    public static string[] insertTypeValues(object dataValues, string RefID)
    {
        int n = Hipocrate.DB.DBConnection.Execute("delete from REFRESULTS_TYPES_REFS where [RefID]=" + RefID);

        object[] newArr = (object[])dataValues;

        for (int i = 0; i < newArr.Length; ++i)
        {
            Dictionary<string, object> tempDic = newArr[i] as Dictionary<string, object>;
            int x = Hipocrate.DB.DBConnection.Execute("insert into REFRESULTS_TYPES_REFS values(" + tempDic["RefResID"] + "," + RefID + ")");
        }

        DataSet ds_getType = Hipocrate.DB.DBConnection.GetData("select * from vRefResults_Types_Refs where [RefID]=" + RefID);
        DataView dv = ds_getType.Tables[0].DefaultView;

        string[] dataTypes = new string[dv.Count];
        for (int i = 0; i < dv.Count; ++i)
            dataTypes[i] = dv.Table.Rows[i]["TypeName"].ToString() + ";" + dv.Table.Rows[i]["TypeId"].ToString() + ";";
        return dataTypes;
    }
}
